import { isError } from 'lodash'
import * as request from 'request-promise'

import { logger } from '@pkg/logger'
import { nothrow } from '@pkg/promise'

export interface Message {
    type: string
    symbol: string
    from: string
    to: string
    hash: string
    amount: string
    blockNumber: number | null
}

/**
 * 发送通知
 * @param url - 通知url
 * @param msg - 交易信息
 */
export async function send(url: string, msg: Message) {
    const response = await nothrow(request.post(url, {
        headers: {
            'Content-Type': 'application/json'
        },
        timeout: 5000,
        body: JSON.stringify(msg)
    }).promise())

    if (isError(response)) {
        logger.error('[Notify] failed to send notify: %s, %s', response.message, JSON.stringify(msg))
    }
}
